package org.apogee.mil.client.widgets;

import java.util.ArrayList;
import java.util.List;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.client.utils.Template;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ServerConfig extends LayoutContainer {

	private FormData formData;  
	private VerticalPanel vp;
	private String selectedDb = null;
	private int sessionId = -1;

	public ServerConfig() {
		sessionId = Session.getSessionId();
	}

	@Override  
	protected void onRender(Element parent, int index) {  
		super.onRender(parent, index);
		formData = new FormData("-20");  
		vp = new VerticalPanel();  
		vp.setSpacing(10);  
		createForm1();   
		add(vp);    
	}

	private void createForm1() {

		FormPanel fp = new FormPanel();
		fp.setHeading("Server Configuration");
		fp.setFrame(true);
		fp.setWidth(500);
		fp.setLabelWidth(150);
		fp.setLabelAlign(LabelAlign.RIGHT);

		final ListStore<BaseModel> dbTypeStore = new ListStore<BaseModel>();
		dbTypeStore.add(getDBType());

		final ComboBox<BaseModel> dbType = new ComboBox<BaseModel>();
		dbType.setFieldLabel("Database Type");
		dbType.setEmptyText("Database Type...");
		dbType.setAllowBlank(false);
		dbType.setStore(dbTypeStore);
		dbType.setDisplayField("dbType");
		dbType.setTemplate(Template.getServerConfigTemplate());
		dbType.setTriggerAction(TriggerAction.ALL);
		dbType.addSelectionChangedListener(new SelectionChangedListener<BaseModel>() {

			@Override
			public void selectionChanged(SelectionChangedEvent<BaseModel> se) {
				dbType.select(se.getSelectedItem());
				try {
					selectedDb = se.getSelectedItem().get("dbType").toString().trim();
				} catch (Exception ex) {
					selectedDb = null;
				} 
			}
		});

		fp.add(dbType);

		final TextField<String> dbName = new TextField<String>();
		dbName.setFieldLabel("Database Name");
		dbName.setAutoWidth(true);
		dbName.setAllowBlank(false);

		fp.add(dbName, formData);
		
		final TextField<String> dbUser = new TextField<String>();
		dbUser.setFieldLabel("Database User");
		dbUser.setAutoWidth(true);
		dbUser.setAllowBlank(false);
		
		fp.add(dbUser, formData);
		
		final TextField<String> dbPwd = new TextField<String>();
		dbPwd.setFieldLabel("Database Password");
		dbPwd.setPassword(true);
		dbPwd.setAllowBlank(false);
		dbPwd.setAutoWidth(true);
		
		fp.add(dbPwd, formData);
		
		final TextField<String> dbUrl = new TextField<String>();
		dbUrl.setFieldLabel("Database URL");
		dbUrl.setAllowBlank(false);
		dbUrl.setAutoWidth(true);
		
		fp.add(dbUrl, formData);
		
		final TextField<String> dbPort = new TextField<String>();
		dbPort.setFieldLabel("Database Port");
		dbPort.setAllowBlank(false);
		dbPort.setAutoWidth(true);
		
		fp.add(dbPort, formData);
		
		Button save = new Button("Save Configuration");
		save.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				
				if (selectedDb == null) {
					MessageBox.alert("Error", "Unable to get Database Type", null);
					return;
				}
				
				String db = dbName.getValue().trim();
				
				String user = dbUser.getValue().trim();
				
				String pass = dbPwd.getValue().trim();
				
				String url = dbUrl.getValue().trim();
				
				String port = dbPort.getValue().trim();
				
				String config = selectedDb + ":" + db + ":" + user + ":" + pass + ":" + url + ":" + port;
				
				ServerServiceFactory.getGreetService(sessionId).saveConfig(config, new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							MessageBox.info("Success", "Configuration File Saved Successfully\nPlease Reload Page", null);
							return;
						}
						
						MessageBox.alert("Error", "Unable to save Configuration File.", null);
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("Error", "Unable to save Configuration File.\n" + caught.getMessage(), null);
					}
				});
				
			}
		});
		
		fp.addButton(save);
		fp.setButtonAlign(HorizontalAlignment.CENTER);
		
		FormButtonBinding binding = new FormButtonBinding(fp);
		binding.addButton(save);


		add(fp);

	}

	private List<BaseModel> getDBType() {
		ArrayList<BaseModel> db = new ArrayList<BaseModel>();

		BaseModel bm = new BaseModel();
		bm.set("dbType", "Oracle");

		db.add(bm);

		bm = new BaseModel();
		bm.set("dbType", "MySQL");
		
		db.add(bm);

		return db;
	}

}
